<template>
  <div class="list" ref="wrapper">
    <div>
      <div>
        <div class="title border-topbottom">当前城市</div>
        <div class="btn-list">
          <div class="btn-wrapper">
            <div class="btn">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div>
        <div class="title border-topbottom">热门城市</div>
        <div class="btn-list">
          <div
            class="btn-wrapper"
            v-for="item of hotCity"
            :key="item.id"
            @click="handleCityClick(item.name)">
            <div class="btn">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div
        v-for="(item, key) of allCity"
        :key="key"
        :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div
          class="item-content border-bottom"
          v-for="innerItem of item"
          :key="innerItem.id"
          @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Bscroll from 'better-scroll'
  import { mapState, mapMutations } from 'vuex'
  export default {
    name: 'cityList',
    props: {
      hotCity: Array,
      allCity: Object,
      targetText: String
    },
    data (){
      return {}
    },
    methods: {
      handleCityClick (city){
        // this.$store.dispatch('changeCity', city)
        // this.$store.commit('CHANGE_CITY', city)
        this.CHANGE_CITY(city)
        this.$router.push('/')
      },
      ...mapMutations(['CHANGE_CITY'])
    },
    mounted(){
      this.$nextTick(()=>{
        this.scroll = new Bscroll(this.$refs.wrapper)
      })
    },
    computed: {
      ...mapState({
        currentCity : 'city'
      })
    },
    watch: {
      targetText (){
        if (this.targetText) {
          const element = this.$refs[this.targetText][0]
          this.scroll.scrollToElement(element)
        }
      }
    }
  }
</script>

<style lang="stylus" scoped>
  @import '~@S/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .list
    position: absolute
    top: 1.58rem
    bottom: 0
    left: 0
    right: 0
    overflow: hidden
    .title
      line-height: .44rem
      background: #eee
      padding-left: .2rem
      color: #666
      font-size: .26rem
    .btn-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .btn-wrapper
        float: left
        width: 33.33%
        .btn
          font-size: .26rem
          border-radius: .1rem
          border: .02rem solid #ccc
          margin: .1rem
          height: .4rem
          line-height: .4rem
          text-align: center
    .item-list
      .item-content
        line-height: .6rem
        color: #666
        padding-left: .2rem
</style>
